define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'room/lists/index' + location.search,
                    add_url: '',
                    edit_url: '',
                    del_url: '',
                    multi_url: '',
                    import_url: '',
                    table: 'room_lists',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'game_id', title: __('Game_id')},
                        // {field: 'subarea_id', title: __('Subarea_id')},
                        // {field: 'modes_id', title: __('Modes_id')},
                        // {field: 'levels_id', title: __('Levels_id')},
                        // {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'game.name', title: __('Game.name')},
                        {field: 'subarea.name', title: __('Subarea.name'), operate: 'LIKE'},
                        {field: 'modes.name', title: __('Modes.name'), operate: 'LIKE'},
                        {field: 'levels.money', title: __('Levels.money'), operate: 'BETWEEN'},
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {"0": "等待中", "1": "对战中", "2": "已结束", "3": "已解散"},
                            formatter: Table.api.formatter.flag
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'updatetime',
                            title: __('Updatetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: "content",
                                    text: "房间内会员",
                                    icon: "fa fa-user fa-fw",
                                    title: "内容",
                                    classname: "btn btn-primary btn-xs btn-dialog btn-content",
                                    url: "room/users?room_id={ids}",
                                    visible: function (row) {
                                        if (row.status == 3) {
                                            return false;
                                        }
                                        return true;
                                    },
                                    refresh: true, //操作完成后是否刷新列表
                                },
                                {
                                    name: 'rank',
                                    text: __('排名'),
                                    title: __('排名'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: '',
                                    url: 'room/lists/rank/id/{ids}',
                                    success: function () {
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status === 0 || row.status === 1) {
                                            return true;
                                        }
                                        return false;
                                    }
                                },
                                {
                                    name: 'jiesan',
                                    text: "解散",
                                    classname: 'btn btn-xs btn-danger btn-ajax',
                                    confirm: '确认解散该房间?',
                                    url: 'room/lists/disband/id/{ids}',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status === 0) {
                                            return true;
                                        }
                                        return false;
                                    },
                                    refresh: true, //操作完成后是否刷新列表
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('load-success.bs.table', function (data) {
                // $(".btn-add").data("area", ["70%", "90%"]);
                $(".btn-content").data("area", ["78%", "75%"]);
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        content: function () {
            Controller.api.bindevent();
        },
        rank: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
